﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>花卉列表</title>
    <script src="/Apm/js/CMSList.js" type="text/javascript"></script>
    <style>
        .list-form {
            padding: 3rem;
        }
    </style>
</head>
<body>
    <div class="page">
        <div class="toolsbar">
            <button id="Add" class="layui-btn">
                <i class="layui-icon">&#xe654;</i> 添加
            </button>
            <button id="Edit" class="layui-btn layui-btn-normal">
                <i class="layui-icon">&#xe642;</i> 编辑
            </button>
            <!--<button id="Delete" class="layui-btn layui-btn-danger">
                <i class="layui-icon">&#xe640;</i> 删除
            </button>-->
        </div>
        <div class="content">
            <table id="tabledata" lay-filter="datalist"></table>
        </div>
    </div>
    <!--<div class="layui-form layui-form-pane list-form">
        <div class="layui-form-item">
            <label class="layui-form-label">时间</label>
            <div class="layui-input-block">
                <input id="TimeFlower" type="text" name="Time" required lay-verify="required" placeholder="请选择时间" autocomplete="off" class="layui-input">

            </div>
        </div>
    </div>-->
</body>
</html>

<script type="text/html" id="TimeTpl">
    {{#
        var FnTime=function(time){
        if(time!=null){
            var date=new Date(time);
            var month=date.getMonth()+1;
            var day=date.getDate();
            if(month-9<1){
                month='0'+month;
            }
            if(day-9<1){
                day='0'+day;
            }
            return date.getFullYear()+'-'+month+'-'+day;
        }
        else{
            return '';
        }
    };
}}
    <span>{{FnTime(d.Time)}}</span>
</script>
<script>
    layui.use(['table', 'layer', 'element','laydate'], function () {
        var table = layui.table,
            layer = layui.layer,
            element = layui.element,
            laydate = layui.laydate;

        BindData();

        //新增
        $("#Add").click(function () {
            var guid = new Guid();
            var flowerguid = guid.newGuid();
            layer.open({
                type: 1,
                title: '时间选择',
                content: '<div class="layui-form layui-form-pane list-form"><div class="layui-form-item"><label class="layui-form-label">时间</label><div class="layui-input-block"><input id="TimeFlower" type="text" name="Time" required lay-verify="required" placeholder="请选择时间" autocomplete="off" class="layui-input"></div></div></div>',
                success: function (layero, index) {
                    //执行一个laydate实例
                    laydate.render({
                        elem: '#TimeFlower' //指定元素
                    });
                },
                btn: ['确定', '关闭'],
                yes: function (index, layero) {
                    var time = $("#TimeFlower").val();
                    if (time == "") {
                        layer.msg('请选择时间');
                    }
                    else {
                        
                        var json = new Object();
                        json.Time = time;
                        json.Guid = flowerguid;
                        //判断所选时间是否存在（后台判断）
                        $.ajax({
                            url: ApiURL + 'api/Method/FlowerAdd?token=' + MyPublic.getToken(),
                            type: 'post',
                            contentType: 'application/json',
                            data: JSON.stringify(json),
                            success: function (data) {
                                var json = JSON.parse(data);
                                
                                if (json.code == '90000') {
                                    layer.msg(json.msg);
                                }
                                else {
                                    if (json.code == '10020') {
                                        BindData();
                                        layer.close(index);
                                        var url = '/Public/Flower/FlowerInfo/FlowerInfoEdit.html?type=add&flowerguid=' + flowerguid + '&time=' + time;
                                        parent.AddTab('PublicFlowerInfoAdd', url, '<img src="/icon/computer_go.png" />', '数据录入');
                                    }
                                    else {
                                        layer.msg(json.msg);
                                    }
                                }
                                
                            },
                            error: function () {
                                location.href = "/error.html";
                            }
                        })

                        
                    }

                }
            });

        })
        //编辑
        $("#Edit").click(function () {
            var checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值

            //console.log(checkStatus.data) //获取选中行的数据
            //console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
            //console.log(checkStatus.isAll) //表格是否全选

            var rows= checkStatus.data.length;
            if (rows > 0) {
                if (rows == 1) {
                    var flowerguid = checkStatus.data[0].Guid;
                    var time = checkStatus.data[0].Time;
                    var remark = checkStatus.data[0].Remark;
                    var url = '/Public/Flower/FlowerInfo/FlowerInfoEdit.html?type=edit&flowerguid=' + flowerguid + '&time=' + time + '&remark=' + remark;
                    parent.AddTab('PublicFlowerInfoEdit', url, '<img src="/icon/computer_go.png" />', '数据录入');
                }
                else {
                    layer.msg("编辑中请不要多选");
                }
            }
            else {
                layer.msg("请选中一行");
            }
            
        })
        //删除
        //$("#Delete").click(function () {
        //    var checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值

        //    var rows = checkStatus.data.length;
        //    if (rows > 0) {
        //        var guidlist = [];
        //        for (var i = 0; i < rows; i++) {
        //            guidlist.push(checkStatus.data[i].Guid);
        //        }
        //        layer.confirm('确定删除？', function (index) {
        //            layer.close(index);
        //            var json = new Object();
        //            json.GuidList = guidlist;

        //            //加载
        //            var loadindex = layer.load(1); 
        //            $.ajax({
        //                url: ApiURL + 'api/Method/DeleteProduct?token=' + MyPublic.getToken(),
        //                type: 'post',
        //                contentType: 'application/json',
        //                data: JSON.stringify(json),
        //                success: function (data) {
        //                    BindData();
        //                    layer.close(loadindex);
        //                },
        //                error: function () {
        //                    location.href = "/error.html";
        //                }
        //            })
        //        });
        //    }
        //    else {
        //        layer.msg("请选择你要删除的项");
        //    }
        //})
    });
    //数据绑定
    function BindData() {
        layui.use(['table', 'layer'], function () {
            var table = layui.table,
                layer = layui.layer;

            //获取列表数据
            $.ajax({
                url: ApiURL + 'api/Method/FlowerList?token=' + MyPublic.getToken(),
                type: 'post',
                contentType: 'application/json',
                success: function (data) {
                    table.render({
                        id: 'datalist',
                        elem: '#tabledata',
                        cols: [[//标题栏
                            { type: 'checkbox' },
                            { field: 'Title', title: '标题', },
                            { field: 'Remark', title: '备注' },
                            { field: 'Time', title: '时间', templet: '#TimeTpl',width:176 }
                        ]],
                        data: data,
                        height: 'full-68',
                        limits: [10, 30, 60],
                        page: true
                    })
                },
                error: function () {
                    location.href = "/error.html";

                }
            })
        });
    }
</script>